package com.material.vtool.service.impl;

import com.material.vtool.entity.BUserMaterialTypeEntity;
import com.material.vtool.entity.combinedEntity.HomePageMaterialTypeEntity;
import com.material.vtool.entity.combinedEntity.SimplyMaterialTypeEntity;
import com.material.vtool.entity.respEntity.ReturnCode;
import com.material.vtool.exception.ServiceException;
import com.material.vtool.mapper.UserMaterialTypeMapper;
import com.material.vtool.service.UserMaterialTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class UserMaterialTypeServiceImpl implements UserMaterialTypeService {
    @Autowired
    private UserMaterialTypeMapper userMaterialTypeMapper;

    @Override
    public List<HomePageMaterialTypeEntity> getHomePageMaterialTypeList(Integer userId, String miniCode) {
        return userMaterialTypeMapper.getHomePageMaterialTypeList(userId, miniCode);
    }

    @Override
    public List<SimplyMaterialTypeEntity> getUserMaterialType(Integer userId, String miniCode) {
        return userMaterialTypeMapper.getUserMaterialTypeByStatus(userId, miniCode, 1);
    }

    @Override
    public List<SimplyMaterialTypeEntity> getHotMaterialType(Integer userId, String miniCode) {
        return userMaterialTypeMapper.getUserMaterialTypeByStatus(userId, miniCode, 0);
    }

    @Override
    public int initializeUserMaterialType(Integer userId, String miniCode) {
        return userMaterialTypeMapper.initializeUserMaterialType(userId, miniCode);
    }

    @Override
    public int getUserMaterialTypeMinSort(Integer userId, String miniCode) {
        Integer userMaterialTypeMinSort = userMaterialTypeMapper.getUserMaterialTypeMinSort(userId, miniCode);
        return Optional.ofNullable(userMaterialTypeMinSort).orElse(0);
    }

    @Override
    public BUserMaterialTypeEntity getUserMaterialTypeByMaterialTypeId(Integer userId, Integer materialTypeId) {
        return userMaterialTypeMapper.getUserMaterialTypeByMaterialTypeId(userId, materialTypeId);
    }

    @Override
    public int updateUserMaterialType(BUserMaterialTypeEntity entity) {
        return userMaterialTypeMapper.updateUserMaterialType(entity);
    }

    @Override
    public int addUserMaterialType(BUserMaterialTypeEntity entity) {
        int affectedCount = userMaterialTypeMapper.addUserMaterialType(entity);
        if (affectedCount == 0) throw new ServiceException("添加频道失败", ReturnCode.SERVER_ERROR);
        return affectedCount;
    }

    @Override
    public int deleteUserMaterialType(Integer materialTypeId) {
        int affectedCount = userMaterialTypeMapper.deleteUserMaterialType(materialTypeId);
        if (affectedCount == 0) throw new ServiceException("数据不存在", ReturnCode.CLIENT_ERROR);
        return affectedCount;
    }

    @Override
    public int resortUserMaterialType(BUserMaterialTypeEntity entity) {
        int affectedCount = userMaterialTypeMapper.resortUserMaterialType(entity);
        if (affectedCount == 0) throw new ServiceException("更新失败", ReturnCode.SERVER_ERROR);
        return affectedCount;
    }

    @Override
    @Transactional
    public int saveUserMaterialType(Integer userId, List<SimplyMaterialTypeEntity> list) {
        // 删除用户的频道
        userMaterialTypeMapper.deleteMaterialTypeByUserId(userId);

        // 保存用户的频道
        int affectedCount = 0;
        if (list != null && list.size() > 0) {
            int sort = 1;
            List<BUserMaterialTypeEntity> bUserMaterialTypeEntityList = new ArrayList<>();
            for (SimplyMaterialTypeEntity entity : list) {
                BUserMaterialTypeEntity bUserMaterialTypeEntity = new BUserMaterialTypeEntity();
                bUserMaterialTypeEntity.setUserId(userId);
                bUserMaterialTypeEntity.setMaterialTypeId(entity.getId());
//                bUserMaterialTypeEntity.setStatus(1);
                bUserMaterialTypeEntity.setSort(sort);
                bUserMaterialTypeEntity.setCreateTime(new Date());

                bUserMaterialTypeEntityList.add(bUserMaterialTypeEntity);
                sort++;
            }
            affectedCount = userMaterialTypeMapper.insertUserMaterialType(bUserMaterialTypeEntityList);
            if (affectedCount == 0) throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);
        }
        return affectedCount;
    }

    @Override
    @Transactional
    public int saveUserMaterialTypeArray(Integer userId, Integer[] materialTypeIds) {
        // 删除用户的频道
        userMaterialTypeMapper.deleteMaterialTypeByUserId(userId);

        // 保存用户的频道
        int affectedCount = 0;
        if (materialTypeIds != null && materialTypeIds.length > 0) {
            // 去重处理
            List<Integer> list = new ArrayList<>();
            int length = materialTypeIds.length;
            for (int i = 0; i < length; i++) {
                Integer materialId = materialTypeIds[i];
                if (!list.contains(materialId)) {
                    list.add(materialId);
                }
            }
            Object[] newMaterialTypeIds = list.toArray();

            // 构造实体对象
            List<BUserMaterialTypeEntity> bUserMaterialTypeEntityList = new ArrayList<>();
            int newLength = newMaterialTypeIds.length;
            for (int j = 0; j < newLength; j++) {
                BUserMaterialTypeEntity bUserMaterialTypeEntity = new BUserMaterialTypeEntity();
                bUserMaterialTypeEntity.setUserId(userId);
                bUserMaterialTypeEntity.setMaterialTypeId((Integer) newMaterialTypeIds[j]);
//                bUserMaterialTypeEntity.setStatus(1);
                bUserMaterialTypeEntity.setSort(j);
                bUserMaterialTypeEntity.setCreateTime(new Date());

                bUserMaterialTypeEntityList.add(bUserMaterialTypeEntity);
            }
            affectedCount = userMaterialTypeMapper.insertUserMaterialType(bUserMaterialTypeEntityList);
            if (affectedCount == 0) throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);
        }
        return affectedCount;
    }

    /**
     * 获取用户订阅频道 ID 列表
     * @param userId 用户 ID
     * @param miniCode 小程序标识码
     * @return
     */
    @Override
    public List<Integer> getUserMaterialTypeIdList(Integer userId, String miniCode) {
        return userMaterialTypeMapper.getUserMaterialTypeIdList(userId, miniCode);
    }
}
